chrome.contextMenus.create({ "title": "ClickTrans", "contexts": ["selection"], "onclick": translate });
function translate() {
	chrome.tabs.getSelected(null, function(tab){
	chrome.tabs.sendRequest(tab.id, {}, function handler(response) {
		});
	});
}


chrome.extension.onRequest.addListener(onRequest);
function onRequest(request, sender, callback) {
	if (request.action == 'translateTxt') {
		translateTxt(request.url, callback);
	}
};
function translateTxt(url,callback) {
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function(data) {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var data = JSON.parse(xhr.responseText);
				alert(data.data.translations[0].translatedText);
				callback(data);
			} else {
				callback(null);
			}
		}
	}
	xhr.open('GET', url, true);
	xhr.send();
};